/*
 * @FileName: BusinessValidator.java
 * @Author
 * @Description:
 * @Date 2016年1月11日 上午11:10:36
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.validator.admin;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.validate.Validator;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.Website;

/**
 * Class description goes here
 *
 * @author
 */
public class WebsiteValidator extends Validator{

    final static String WEBSITEID_MESSAGE = "websiteidMessages";

    @Override
    protected void validate(Controller c){

        if(!Website.dao.checkUnique(c.getParaToInt(Website.ID), c.getParaToInt(Website.WEBSITE_ID))){
            addError(WEBSITEID_MESSAGE, "网点编号已存在");
        }

        validateRegex(Website.WEBSITE_ID, Constant.Regex.NUMBER, WEBSITEID_MESSAGE, "请输入网点编号(数字)");

    }

    @Override
    protected void handleError(Controller c){
        c.keepPara();
        c.setAttr("edit", c.getPara("edit", "0"));
        Record pager;
        if(c.getParaToInt(Website.ID, 0) == 0){
            c.setAttr("title", c.getPara("title", "新增网点类型"));
            pager = new Record();
            pager.set(Website.ID, 0);
            pager.set(Website.WEBSITE_ID, c.getParaToInt(Website.WEBSITE_ID, 0));
            pager.set(Website.WEBSITE_NAME, c.getPara(Website.WEBSITE_NAME, ""));
        } else{
            c.setAttr("title", c.getPara("title", "编辑网点类型"));
            pager = Db.paginate(c.getParaToInt(Constant.Ui.PAGER_NUMBER, 1), 1, "select *",
                                "from " + Constant.Table.ORGANIZATION_CLASS + " where " + Website.ID + "=?",
                                new Object[] { c.getParaToInt(Website.ID, 0) })
                    .getList().get(0);

        }
        c.setAttr(Constant.Ui.PAGER, pager);
        c.render("/admin/website_add.tpl");
    }
}
